<template>
  <el-dialog
    :close-on-click-modal="false"
    title="银行收款台账"
    :visible.sync="show"
    width="800px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" :disabled="disableForm">
              <i slot="suffix" class="el-input__icon el-icon-circle-plus-outline" @click="optType === 'add' ? showStoreModal = true : null" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款编号" prop="number">
            <el-input v-model="form.number" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款金额" prop="amount">
            <el-input v-model="form.amount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款类型" prop="type">
            <el-select v-model="form.type" :disabled="disableForm" style="width: 100%;" placeholder="请选择收款类型">
              <el-option v-for="item in projectBankCollectionType" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="描述" prop="description">
            <el-input v-model="form.description" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>

      <div class="com-form-title">附件</div>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
      <projectNameList :show-modal.sync="showStoreModal" @getSelectedData="getStoreSelectedData" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addProjectBankCollection, updateProjectBankCollection, getProjectBankCollectionDetail } from '@/api/moneyManage'
// import { getAreaList } from '@/api/common'
import { projectBankCollectionType, workflowStatus } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
import projectNameList from './projectNameList.vue'
export default {
  name: 'AddProjectBankCollection',
  components: {
    fileUploader,
    projectNameList
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'detail'
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      selectRow: null,
      targetList: [],
      projectBankCollectionType,
      workflowStatus,
      showStoreModal: false,
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        projectName: '',
        number: '',
        amount: '',
        type: '',
        description: '',
        addWorkflowId: '',
        status: '',
        founderName: '',
        createTime: ''
      },
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '单位名称', trigger: 'change' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.id) {
          this.getDocDetail(newData.businessId)
        }
      },
      deep: true,
      immediate: true
    },
    showModal(val) {
      if (val) {
        if (this.optType === 'edit') {
          this.getDocDetail(this.data.id)
        }
      }
    }
  },
  mounted() {

  },
  methods: {
    getStoreSelectedData(type, data) {
      if (type === 'store') {
        this.form.projectName = data.projectInfoName
      }
    },
    getDocDetail(id) {
      getProjectBankCollectionDetail({ projectBankCollectionId: id }).then(res => {
        const { lstAttachment, projectBankCollection } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        Object.keys(this.form).forEach(key => {
          const source = projectBankCollection[key]
          if (typeof source === 'boolean') {
            this.form[key] = source ? 1 : 0
          } else {
            this.form[key] = source
          }
        })
      })
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = this.optType === 'add' ? {
            projectBankCollection: {
              ...this.form
            },
            lstAttachment: this.fileList
          } : {
            ...this.form,
            id: this.data.id
          }
          const fn = this.optType === 'add' ? addProjectBankCollection : updateProjectBankCollection
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
